Ext.define('Admin.view.trade.ContractPlanListController', {
	        extend : 'Admin.app.CrudController',
	        alias : 'controller.contractplanlist',

	        onContractPlanListRefresh : function(view, record, params) {
		        var me = this, vm = me.getViewModel();
		        var store = vm.getStore('contractplanListStore');
//		        var searchDate = Ext.getCmp('searchcontractDate').getValue();
//		        var date = Ext.util.Format.date(searchDate,'Y-m-d');
		        store.proxy.extraParams = {
				         id : record.data.id,
//				         level:record.data.level
//				         date:date
				         };
		        store.reload();
	        },

	        onInsertClick : function(btn) {
		        var me = this, grid = btn.ownerCt.ownerCt, parent = me.getViewModel().get('record');
		        if (!parent)
			        return;
		        var record = Ext.create(grid.getStore().model, {
			                id : null,
			                name : '新建合同计划电量',
			                market : parent.data.id
		                });
		        record.store = grid.getStore();
		        me.showDetail(grid.detail, record);
	        },

	        afterDetail : function(record, win) {
		        var me = this, vm = me.getViewModel(), store = vm.getStore('programStore');
		        win.getViewModel().set('programStore', store);
	        },

	        onTradeResult : function(view, rowIndex, colIndex, item, e, record, row) {
	        	var trade = view.ownerCt.ownerCt.ownerCt;
	        	trade.getController().showTradeResult(record.data);
	        }
	        ,
	        onOk : function(button) {
	            var me = this, form = button.ownerCt.ownerCt, win = form.ownerCt, vm = win.getViewModel();
	            var cov = form.items.items;
	            var json = "";
	            var json1 ="^";
	            var data =  cov[0].getValues();
			    json+="{";
			    if(cov.length>1){
				if(data.tradeTime) {json+="\"tradeTime\":"+data.tradeTime+",";}
				if(data.tradeWay) {json+="\"tradeWay\":"+data.tradeWay+",";}
				if(data.electricityType) {json+="\"electricityType\":"+data.electricityType+",";}

	            for( i=0;i<cov.length;i++){
	            	var data= cov[i].getValues();
					if(i>0){
	            			var cov1 = cov[i].items.items;
	            			for(j=0;j<cov1.length;j++){
	            				var data1 = cov1[j].getValues();
	            				if(j!=cov1.length-1){
	            					var json2= json; 	 			    				
									if(data1.tradeVariety) {json2+="\"tradeVariety\":"+data1.tradeVariety+",";}
									if(data1.tradeArea) {json2+="\"tradeArea\":"+data1.tradeArea+",";}
									if(data1.energy) {json2+="\"energy\":"+data1.energy+",";}
									if(data1.price) {json2+="\"price\":"+data1.price+",";}
									if(data1.fee) {json2+="\"fee\":"+data1.fee+",";} 
									if(data1.org) {json2+="\"orgPlant\":"+data1.org+",";}
									json2+="\"args\":\""+0+"\"";
									json1+=json2+"},";
								}else{		
									var json3= json;
									if(data1.tradeVariety) {json3+="\"tradeVariety\":"+data1.tradeVariety+",";}
									if(data1.tradeArea) {json3+="\"tradeArea\":"+data1.tradeArea+",";}
									if(data1.energy) {json3+="\"energy\":"+data1.energy+",";}
									if(data1.price) {json3+="\"price\":"+data1.price+",";}
									if(data1.fee) {json3+="\"fee\":"+data1.fee+",";} 
									if(data1.org) {json3+="\"orgPlant\":"+data1.org+",";}
									json3+="\"args\":\""+0+"\"";
									json1+=json3+"}";

								}
	            			}
	            		
	            	  }
	            	}
			    }else{
			    	alert("请添加合同计划……");
			    	return;
			    }
	            json1+="*";
	            Ext.Ajax.timeout=7200000;
	            Ext.Ajax.request({
	        		url:"rest/api/contractPlan/saveall/"+json1,
	        		method:"PUT",
	    			headers:{
	                    'Content-Type': 'application/json; charset=utf-8'
	    					},
	        		success:function(response,options ){
	        			var result = response.responseText;
	        			if(result==1){
	        				alert("添加成功");
	        			}
	        		}
	        	});
	        },
	        onParamsChange : function(cmp, newValue, oldValue, eOpts) {
				var me = this, vm = me.getViewModel(), view = me.getView(), record = vm.get('record');
				if (view.isVisible() && cmp.isVisible() && record) { // 判断isVisible()，防止页面未构建但是因为赋初值而误动作
					// debugger;
					var params = {};
					params[cmp.getBind().value.stub.name] = newValue;
					view.fireEvent('refresh', view, record, params);
				}
			},
			onPrevClick : function(btn) {
				var me = this, vm = me.getViewModel(), date = vm.get('date');
				vm.set('date', Ext.Date.add(date, Ext.Date.MONTH, -1));
			},

			onNextClick : function(btn) {
				var me = this, vm = me.getViewModel(), date = vm.get('date');
				vm.set('date', Ext.Date.add(date, Ext.Date.MONTH, 1));
			}
        });